package cc.idoone.joddhttp.demo;

import jodd.http.HttpRequest;
import jodd.http.HttpResponse;

import java.util.HashMap;

/**
 * @author hemj
 * @version 2017/7/31
 */
public class GetRequestDemo {
    private static final String URL = "http://localhost:8088/joddhttp/test";

    public static void main(String[] args) {
        HttpRequest httpRequest = HttpRequest.get(URL); //1. 构建一个get请求
        HttpResponse response = httpRequest.send(); //2.发送请求并接受响应信息
        System.out.println(response);
        System.out.println(response.charset("UTF-8").bodyText()); //3.打印响应信息

        // 或者一步步构建
        HttpRequest request = new HttpRequest();
        request
                .method("GET")
                .protocol("http")
                .host("localhost")
                .port(8088)
                .path("/joddhttp/test");
        System.out.println(request.send().charset("UTF-8").bodyText());

        // 带查询参数
        System.out.println(HttpRequest.get(URL + "?uid=1")
                .send().charset("UTF-8").bodyText());

        // 带查询参数
        HashMap<String, String> queryMap = new HashMap<>();
        queryMap.put("query", "queryValue");
        System.out.println(HttpRequest.get(URL + "?uid=1")
                .query("uname", "hemj")
                .query("param1", "value1", "param2", "value2")
                .query(queryMap)
                .send().charset("UTF-8").bodyText());
    }
}
